package com.whmskj.xjlhbc.mobile.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.domain.Bctj18wshopydday;
import com.whmskj.xjlhbc.domain.Bctj18wshopydqk;
import com.whmskj.xjlhbc.mapper.Bctj18wshopyddayMapper;
import com.whmskj.xjlhbc.mapper.Bctj18wshopydqkMapper;
import com.whmskj.xjlhbc.mobile.GetStartDate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * 用电月报
 */
@RestController
@RequestMapping("/ydyb")
public class YdqkController {

    @Autowired
    private Bctj18wshopyddayMapper bctj18wshopyddayMapper;

    @Autowired
    private Bctj18wshopydqkMapper bctj18wshopydqkMapper;

    @GetMapping("monthYdqk")
    @Operation(description = "用电月报",parameters = @Parameter(name = "date",description = "日期,yyyy-MM"))
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult monthYdqk(@RequestParam(required = false) String month){
        int year = DateUtil.date().year();
        int monthValue = GetStartDate.getMonthValue();
        if(StringUtils.hasText(month)){
            String[] split = month.split("-");
            year = NumberUtil.parseInt(split[0], year);
            monthValue = NumberUtil.parseInt(split[1], monthValue);
        }
        LambdaQueryWrapper<Bctj18wshopydqk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bctj18wshopydqk::getYear, year).eq(Bctj18wshopydqk::getMonth, monthValue).last("limit 1");
        Bctj18wshopydqk bctj18wshopydqk = bctj18wshopydqkMapper.selectOne(wrapper);
        Bctj18wshopydqk result = Optional.ofNullable(bctj18wshopydqk).orElseGet(Bctj18wshopydqk::new);
        return AjaxResult.success(result);
    }

    @GetMapping("dayYdqk")
    @Operation(description = "用电月报",parameters = @Parameter(name = "date",description = "日期,yyyy-MM"))
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class})
    ))
    public AjaxResult dayYdqk(@RequestParam(required = false) String month){
        String start = GetStartDate.getMonthStartDate().substring(0,10);
        String end = DateUtil.today();
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(month)){
            LocalDate ld = LocalDate.parse(month+"-21").minusMonths(1);
            start = ld.withDayOfMonth(21).toString();
            end = LocalDate.parse(month+"-20").toString();
        }
        LambdaQueryWrapper<Bctj18wshopydday> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Bctj18wshopydday::getStatisticsDate, start, end).orderByDesc(Bctj18wshopydday::getStatisticsDate);
        List<Bctj18wshopydday> result = bctj18wshopyddayMapper.selectList(wrapper);
        return AjaxResult.success(result);
    }


}
